<?php

class Sistema_ParametrosController extends App_Controller_Action
{
    private $_table     = 'PARAMETROS';
    private $_pk        = 'PAR_ID';
    private $_order_col = 'PAR_ID';
    private $_order     = 'ASC';
    private $_sta_col   = '';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PAR_FILE'; 
    public  $dir        = 'public/upload/';
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $form = new Sistema_Form_Parametros();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('PAR_ID');
                
                $data = $formData;
                
                unset($data['Salvar']);
                
                $obj = new Sistema_Model_DbTable_Parametros();
                
                if ($id == 0){
                    $obj->insert($data);  
                }
                else{
                    $obj->update($data, 'PAR_ID = ' . $id);
                }
                
                //Atualiza sessão
                $sessao = new Zend_Session_Namespace('Parametros');
                $sessao->parametros = $data;
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
            
            
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Sistema_Model_DbTable_Parametros();
                $form->populate($obj->fetchRow('PAR_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Sistema_Model_DbTable_Parametros();
            $ok = (bool) $obj->delete('PAR_ID = '.(int)$id);
            
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Sistema_Model_DbTable_Parametros();
            //$form->populate($obj->fetchRow('PAR_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

